﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Controls;

namespace StudentManagement.Util.ValidationRules
{
    public class NumericalValidation : ValidationRule
    {
        private readonly string _errorMsg;

        /// <summary>
        /// 验证是否是数值
        /// </summary>
        /// <param name="ErrorMsg">错误消息</param>
        public NumericalValidation(string ErrorMsg)
        {
            _errorMsg = ErrorMsg;
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value != null && value != (object)string.Empty)
            {
                if (!decimal.TryParse(value.ToString(), out decimal _))
                {
                    return new ValidationResult(false, _errorMsg);
                }
            }
            return ValidationResult.ValidResult;
        }
    }
}
